/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.mgt.server.service.power.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.github.yulichang.repository.JoinCrudRepository;
import com.iwindplus.mgt.domain.dto.power.UserPositionDTO;
import com.iwindplus.mgt.server.mapper.power.UserPositionMapper;
import com.iwindplus.mgt.server.model.power.UserPositionDO;
import com.iwindplus.mgt.server.service.power.UserPositionService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户职位关系业务层接口实现类.
 *
 * @author zengdegui
 * @since 2018/9/1
 */
@Service
@CacheConfig(cacheNames = {"userPosition"})
@Transactional(rollbackFor = Exception.class)
public class UserPositionServiceImpl extends JoinCrudRepository<UserPositionMapper, UserPositionDO>
        implements UserPositionService {
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = {"userPosition"}, allEntries = true),
                    @CacheEvict(cacheNames = {"position"}, allEntries = true),
                    @CacheEvict(cacheNames = {"resource"}, allEntries = true),
                    @CacheEvict(cacheNames = {"role"}, allEntries = true),
            }
    )
    @Override
    public boolean save(UserPositionDTO entity) {
        List<UserPositionDO> entities = new ArrayList<>(10);
        UserPositionDO model = BeanUtil.copyProperties(entity, UserPositionDO.class);
        this.buildParam(model, entities);
        if (CollUtil.isNotEmpty(entities)) {
            super.saveBatch(entities, DEFAULT_BATCH_SIZE);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Caching(
            evict = {
                    @CacheEvict(cacheNames = {"userPosition"}, allEntries = true),
                    @CacheEvict(cacheNames = {"position"}, allEntries = true),
                    @CacheEvict(cacheNames = {"resource"}, allEntries = true),
                    @CacheEvict(cacheNames = {"role"}, allEntries = true),
            }
    )
    @Override
    public boolean saveBatchPosition(Long userId, Set<Long> positionIds) {
        if (CollUtil.isEmpty(positionIds)) {
            return Boolean.FALSE;
        }
        List<UserPositionDO> entities = new ArrayList<>(10);
        positionIds.stream().forEach(positionId -> {
            UserPositionDO entity = UserPositionDO.builder()
                    .userId(userId)
                    .positionId(positionId)
                    .build();
            this.buildParam(entity, entities);
        });
        if (CollUtil.isNotEmpty(entities)) {
            super.saveBatch(entities, DEFAULT_BATCH_SIZE);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Caching(
            evict = {
                    @CacheEvict(cacheNames = {"userPosition"}, allEntries = true),
                    @CacheEvict(cacheNames = {"position"}, allEntries = true),
                    @CacheEvict(cacheNames = {"resource"}, allEntries = true),
                    @CacheEvict(cacheNames = {"role"}, allEntries = true),
            }
    )
    @Override
    public boolean saveBatchUser(Long positionId, Set<Long> userIds) {
        if (CollUtil.isEmpty(userIds)) {
            return Boolean.FALSE;
        }
        List<UserPositionDO> entities = new ArrayList<>(10);
        userIds.stream().forEach(userId -> {
            UserPositionDO entity = UserPositionDO.builder()
                    .userId(userId)
                    .positionId(positionId)
                    .build();
            this.buildParam(entity, entities);
        });
        if (CollUtil.isNotEmpty(entities)) {
            super.saveBatch(entities, DEFAULT_BATCH_SIZE);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Caching(
            evict = {
                    @CacheEvict(cacheNames = {"userPosition"}, allEntries = true),
                    @CacheEvict(cacheNames = {"position"}, allEntries = true),
                    @CacheEvict(cacheNames = {"resource"}, allEntries = true),
                    @CacheEvict(cacheNames = {"role"}, allEntries = true),
            }
    )
    @Override
    public boolean saveBatch(List<UserPositionDTO> entities) {
        if (CollUtil.isEmpty(entities)) {
            return Boolean.FALSE;
        }
        List<UserPositionDO> params = new ArrayList<>(10);
        entities.stream().forEach(entity -> {
            UserPositionDO model = BeanUtil.copyProperties(entity, UserPositionDO.class);
            this.buildParam(model, params);
        });
        if (CollUtil.isNotEmpty(params)) {
            super.saveBatch(params, DEFAULT_BATCH_SIZE);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Caching(
            evict = {
                    @CacheEvict(cacheNames = {"userPosition"}, allEntries = true),
                    @CacheEvict(cacheNames = {"position"}, allEntries = true),
                    @CacheEvict(cacheNames = {"resource"}, allEntries = true),
                    @CacheEvict(cacheNames = {"role"}, allEntries = true),
            }
    )
    @Override
    public boolean removeByIds(List<Long> ids) {
        return CollUtil.isNotEmpty(ids) && SqlHelper.retBool(super.baseMapper.deleteByIds(ids));
    }

    @Caching(
            evict = {
                    @CacheEvict(cacheNames = {"userPosition"}, allEntries = true),
                    @CacheEvict(cacheNames = {"position"}, allEntries = true),
                    @CacheEvict(cacheNames = {"resource"}, allEntries = true),
                    @CacheEvict(cacheNames = {"role"}, allEntries = true),
            }
    )
    @Override
    public boolean removeByUserIds(List<Long> userIds) {
        return CollUtil.isNotEmpty(userIds) && SqlHelper.retBool(super.baseMapper.deleteByUserIds(userIds));
    }

    @Caching(
            evict = {
                    @CacheEvict(cacheNames = {"userPosition"}, allEntries = true),
                    @CacheEvict(cacheNames = {"position"}, allEntries = true),
                    @CacheEvict(cacheNames = {"resource"}, allEntries = true),
                    @CacheEvict(cacheNames = {"role"}, allEntries = true),
            }
    )
    @Override
    public boolean removeByPositionIds(List<Long> positionIds) {
        return CollUtil.isNotEmpty(positionIds) && SqlHelper.retBool(super.baseMapper.deleteByPositionIds(positionIds));
    }

    @Caching(
            evict = {
                    @CacheEvict(cacheNames = {"userPosition"}, allEntries = true),
                    @CacheEvict(cacheNames = {"position"}, allEntries = true),
                    @CacheEvict(cacheNames = {"resource"}, allEntries = true),
                    @CacheEvict(cacheNames = {"role"}, allEntries = true),
            }
    )
    @Override
    public boolean editBatchPosition(Long userId, Set<Long> positionIds) {
        this.removeByUserIds(Arrays.asList(userId));
        this.saveBatchPosition(userId, positionIds);
        return Boolean.TRUE;
    }

    @Caching(
            evict = {
                    @CacheEvict(cacheNames = {"userPosition"}, allEntries = true),
                    @CacheEvict(cacheNames = {"position"}, allEntries = true),
                    @CacheEvict(cacheNames = {"resource"}, allEntries = true),
                    @CacheEvict(cacheNames = {"role"}, allEntries = true),
            }
    )
    @Override
    public boolean editBatchUser(Long positionId, Set<Long> userIds) {
        this.removeByPositionIds(Arrays.asList(positionId));
        this.saveBatchUser(positionId, userIds);
        return Boolean.TRUE;
    }

    private void buildParam(UserPositionDO entity, List<UserPositionDO> entities) {
        long count = super.count(Wrappers.lambdaQuery(UserPositionDO.class)
                .eq(UserPositionDO::getUserId, entity.getUserId())
                .eq(UserPositionDO::getPositionId, entity.getPositionId()));
        if (!SqlHelper.retBool(count)) {
            entities.add(entity);
        }
    }
}
